package lz.controls
{
	import com.global.GlobalDef;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;

	/**
	 * 纯文字tips 
	 * @author Administrator
	 * 
	 */	
	public class PlaintextTips extends Sprite
	{
		/**
		 *文本最宽为200; 
		 */
		private static const MAX_WIDTH:int = 200;
		private var _tipsDic:Dictionary;
		private var _text:TextField;
		
		private static var _plainTextTips:PlaintextTips;
//		private var _tempTime:int;
		private var _callBack:Function;
		public function PlaintextTips()
		{
			super();
			init();
		}
		private function init():void
		{
			_tipsDic = new Dictionary();
			_text = new TextField();
			_text.autoSize = TextFieldAutoSize.CENTER;
			_text.multiline = true;
			_text.wordWrap = true;
			_text.mouseEnabled = false;
			_text.htmlText = "tips";
			
			var _tf:TextFormat = new TextFormat();
			_tf.color = 0x000000;
			_text.defaultTextFormat = _tf;
			this.addChild(_text);
			this.mouseEnabled = false;
			this.mouseChildren = false;
		}
		
		/**
		 *绘制 
		 * 
		 */
		public function draw($str:String):void
		{
			_text.htmlText = $str;
			if(_text.textWidth + 10>= MAX_WIDTH)
				_text.width = MAX_WIDTH;
			_text.autoSize = TextFieldAutoSize.CENTER;
			
			this.graphics.clear();
			this.graphics.beginFill(0xFBFBFB,1);
			this.graphics.lineStyle(1,0x000000,0);
			this.graphics.drawRoundRect(-7,-5,_text.textWidth+17 ,_text.textHeight + 13,10,10);
		}
		
		
		
		
		/**
		 * 添加tips 
		 * @param $displayObject显示对象
		 * @param $tips			显示内容 
		 * @param $callBack		回调函数
		 * 
		 */
		public function addTips($displayObject:DisplayObject,$tips:String = null,$callBack:Function = null):void
		{
			if($displayObject == null)return;
			_tipsDic[$displayObject] = {tips:$tips,callBack:$callBack};
			$displayObject.addEventListener(MouseEvent.ROLL_OVER,onOverHandler);//移上时调度
			$displayObject.addEventListener(MouseEvent.ROLL_OUT,onOutHandler);
			$displayObject.addEventListener(MouseEvent.MOUSE_MOVE,onMoveHandler);//在$displayObject上移动调度
		}
		/**
		 * 删除tips 
		 * @param $displayObject
		 * 
		 */
		public function romoveTips($displayObject:DisplayObject):void
		{
			if($displayObject == null)return;
			$displayObject.removeEventListener(MouseEvent.ROLL_OVER,onOverHandler);//移上时调度
			$displayObject.removeEventListener(MouseEvent.ROLL_OUT,onOutHandler);
			$displayObject.removeEventListener(MouseEvent.MOUSE_MOVE,onMoveHandler);//在$displayObject上移动调度
			delete _tipsDic[$displayObject];
		}
		/**
		 * 移上显示对象时调度
		 * @param event
		 * 
		 */
		private function onOverHandler(event:MouseEvent):void
		{
			var displayObj:DisplayObject = event.target as DisplayObject;
			if(_tipsDic[displayObj] == null)return;
			var obj:Object = _tipsDic[displayObj];
			if(obj.tips != null)
			{
				draw(obj.tips);
			}else if(obj.callBack != null)
			{
				_callBack = obj.callBack;
				draw(_callBack());
			}
//			_tempTime = getTimer();
		}
		/**
		 * 移出显示对象时调度
		 * @param event
		 * 
		 */
		private function onOutHandler(event:MouseEvent):void
		{
//			_tempTime = 0
			if(GlobalDef.GameStage.contains(this))
				GlobalDef.GameStage.removeChild(this);
			_callBack = null;
		}
		/**
		 * 在显示对象上移动调度
		 * @param event
		 * 
		 */
		private function onMoveHandler(event:MouseEvent):void
		{
			var mousePoint:Point = new Point(mouseX,mouseY);
			var globPoint:Point = localToGlobal(mousePoint);
			this.x = globPoint.x + 15;
			this.y = globPoint.y +20;
			if(this.x + this.width >= GlobalDef.GameStage.stageWidth)
				this.x = globPoint.x - _text.textWidth - 10;
			if(this.y + this.height >= GlobalDef.GameStage.stageHeight)
				this.y = globPoint.y - _text.textHeight ;
			
			if(_callBack != null)draw(_callBack());
			
			GlobalDef.GameStage.addChild(this);
			
		}
		/**
		 *动态改变内容 
		 * @param $str
		 * 
		 */
		public function changeInfo($str:String,$callBack:Function):void
		{
			if(this.parent == null || _callBack != $callBack )return;
			_text.text = $str;
		}
		public static function get instance():PlaintextTips
		{
			if(_plainTextTips == null)_plainTextTips = new PlaintextTips();
			return _plainTextTips;
		}
	}
}